#include "StdAfx.h"
#include "HttpBuffer.h"

namespace HttpProtocol
{

CHttpBuffer::CHttpBuffer(CHttpStream* pHttpFile)
{
	_ASSERT(pHttpFile != NULL);

	string strBytesReceived;
	for (m_lByteSize = 0; ;)
	{
		char cReadBuffer[4096] = { 0 };
		unsigned long lNumberOfBytesRead;
		BOOL bResult = pHttpFile->ReadStream(cReadBuffer,sizeof(cReadBuffer) - 1,&lNumberOfBytesRead);
		if(!bResult || !lNumberOfBytesRead)
			break;
		cReadBuffer[lNumberOfBytesRead] = 0;
		strBytesReceived += cReadBuffer;
		m_lByteSize += lNumberOfBytesRead;
	}

	m_lpOutBuffer = _strdup(strBytesReceived.c_str());
}

CHttpBuffer::~CHttpBuffer(void)
{
	if (m_lpOutBuffer != NULL)
		delete[] m_lpOutBuffer;
}

CHttpBuffer::operator const char*()
{
	return (const char *)GetStream();
}

LPVOID CHttpBuffer::GetStream()
{
	return m_lpOutBuffer;
}

DWORD CHttpBuffer::GetSize(void)
{
	return m_lByteSize;
}

}